package com.example.demo.controller;

import com.example.demo.pojo.Topic;
import com.example.demo.service.ITopicService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Writer;
import java.util.List;

@Controller
@RequestMapping("/Topic")
public class TopicController {

    @Autowired
    private ITopicService TopicService;

    @RequestMapping("/selectTopic")
    public void selectTopic(HttpServletResponse response) throws IOException {
        List<Topic> list=TopicService.selectTopic();
        Gson gson = new Gson();
        String json = gson.toJson(list);
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "GET,POST");
        response.setContentType("text/json,charset=utf-8");
        response.setCharacterEncoding("utf-8");
        Writer out = response.getWriter();
        out.write(json);
        out.flush();
    }

    @RequestMapping("/addTopic")
    private void addTopic( HttpServletResponse response,Topic Topic, String title, Integer tid) throws IOException {
        Topic.setTitle(title);
        Topic.setTid(tid);
        Topic.setIcon(0);
        TopicService.insertSelective(Topic);
        Gson gson = new Gson();
        String json = gson.toJson("");
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "GET,POST");
        response.setContentType("text/json,charset=utf-8");
        response.setCharacterEncoding("utf-8");
        Writer out = response.getWriter();
        out.write(json);
        out.flush();
    }

    @RequestMapping("/updateTopic")
    private void updateTopic(HttpServletResponse response, Topic Topic, String analysis, Integer id, String ifc, String leixing, String title, Integer tid) throws IOException {
        Topic.setTitle(title);
        Topic.setId(id);
        TopicService.updateByPrimaryKeySelective(Topic);
        Gson gson = new Gson();
        String json = gson.toJson("");
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "GET,POST");
        response.setContentType("text/json,charset=utf-8");
        response.setCharacterEncoding("utf-8");
        Writer out = response.getWriter();
        out.write(json);
        out.flush();
    }

    @RequestMapping("/deleteTopic")
    private void deleteTopic(HttpServletResponse response, Topic Topic, Integer id) throws IOException {
        TopicService.deleteByPrimaryKey(id);
        Gson gson = new Gson();
        String json = gson.toJson("");
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "GET,POST");
        response.setContentType("text/json,charset=utf-8");
        response.setCharacterEncoding("utf-8");
        Writer out = response.getWriter();
        out.write(json);
        out.flush();
    }

    @RequestMapping("/selectTopicByIfc")
    public void selectTopicByIfc(HttpServletResponse response, String ifc) throws IOException {
        List<Topic> list=TopicService.selectTopicByIfc(ifc);
        Gson gson = new Gson();
        String json = gson.toJson(list);
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "GET,POST");
        response.setContentType("text/json,charset=utf-8");
        response.setCharacterEncoding("utf-8");
        Writer out = response.getWriter();
        out.write(json);
        out.flush();
    }

    @RequestMapping("/selectTopicByTid")
    public void selectTopicByTid(HttpServletResponse response,Integer tid) throws IOException {
        List<Topic> list=TopicService.selectTopicByTid(tid);
        Gson gson = new Gson();
        String json = gson.toJson(list);
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "GET,POST");
        response.setContentType("text/json,charset=utf-8");
        response.setCharacterEncoding("utf-8");
        Writer out = response.getWriter();
        out.write(json);
        out.flush();
    }

    @RequestMapping("/selectTopicByIfcLeixing")
    public void selectTopicByIfcLeixing(HttpServletResponse response, String leixing,String ifc) throws IOException {
        List<Topic> list=TopicService.selectTopicByIfcLeixing(ifc,leixing);
        Gson gson = new Gson();
        String json = gson.toJson(list);
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "GET,POST");
        response.setContentType("text/json,charset=utf-8");
        response.setCharacterEncoding("utf-8");
        Writer out = response.getWriter();
        out.write(json);
        out.flush();
    }

    @RequestMapping("/selectTopicByIfcNdu")
    public void selectTopicByIfcNdu(HttpServletResponse response, Integer ndu,String ifc) throws IOException {
        List<Topic> list=TopicService.selectTopicByIfcNdu(ifc,ndu);
        Gson gson = new Gson();
        String json = gson.toJson(list);
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "GET,POST");
        response.setContentType("text/json,charset=utf-8");
        response.setCharacterEncoding("utf-8");
        Writer out = response.getWriter();
        out.write(json);
        out.flush();
    }

    @RequestMapping("/selectTopicByIfcSuiji")
    public void selectTopicByIfcSuiji(HttpServletResponse response, String ifc) throws IOException {
        List<Topic> list=TopicService.selectTopicByIfcSuiji(ifc);
        Gson gson = new Gson();
        String json = gson.toJson(list);
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "GET,POST");
        response.setContentType("text/json,charset=utf-8");
        response.setCharacterEncoding("utf-8");
        Writer out = response.getWriter();
        out.write(json);
        out.flush();
    }

    @RequestMapping("/selectTopicById")
    public void selectTopic(HttpServletResponse response, Integer id) throws IOException {
        Gson gson = new Gson();
        String json = gson.toJson(TopicService.selectByPrimaryKey(id));
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "GET,POST");
        response.setContentType("text/json,charset=utf-8");
        response.setCharacterEncoding("utf-8");
        Writer out = response.getWriter();
        out.write(json);
        out.flush();
    }
}
